﻿using DBUtil;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL.Permission
{
    public class ExtendedPermissionDAL
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByType(int TypeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[ExtendedPermission]");
            strSql.Append(" where TypeId=@TypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@TypeID",TypeID)
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID, string Comment)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[ExtendedPermission]");
            strSql.Append(" where [ExtendedPermissionID]!=@ExtendedPermissionID and Comment=@Comment");
            SqlParameter[] parameters = {
					new SqlParameter("@ExtendedPermissionID",ID),
                    new SqlParameter("@Comment",Comment)
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string Comment)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[ExtendedPermission]");
            strSql.Append(" where Comment=@Comment");
            SqlParameter[] parameters = {
                    new SqlParameter("@Comment",Comment)
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ExtendedPermissionEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Permission].[ExtendedPermission](");
            strSql.Append("TypeId,Comment,CustomMessage)");
            strSql.Append(" values (");
            strSql.Append("@TypeId,@Comment,@CustomMessage)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@TypeId", SqlDbType.Int,4),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@CustomMessage", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.TypeId;
            parameters[1].Value = model.Comment;
            parameters[2].Value = model.CustomMessage;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ExtendedPermissionEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Permission].[ExtendedPermission] set ");
            strSql.Append("TypeId=@TypeId,");
            strSql.Append("Comment=@Comment,");
            strSql.Append("CustomMessage=@CustomMessage");
            strSql.Append(" where ExtendedPermissionID=@ExtendedPermissionID");
            SqlParameter[] parameters = {
					new SqlParameter("@TypeId", SqlDbType.Int,4),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@CustomMessage", SqlDbType.NVarChar,200),
					new SqlParameter("@ExtendedPermissionID", SqlDbType.Int,4)};
            parameters[0].Value = model.TypeId;
            parameters[1].Value = model.Comment;
            parameters[2].Value = model.CustomMessage;
            parameters[3].Value = model.ExtendedPermissionID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ExtendedPermissionID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[ExtendedPermission] ");
            strSql.Append(" where ExtendedPermissionID=@ExtendedPermissionID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExtendedPermissionID", SqlDbType.Int,4)
			};
            parameters[0].Value = ExtendedPermissionID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ExtendedPermissionIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[ExtendedPermission] ");
            strSql.Append(" where ExtendedPermissionID in (" + ExtendedPermissionIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ExtendedPermissionEntity GetModel(int ExtendedPermissionID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ExtendedPermissionID,TypeId,Comment,CustomMessage from [Permission].[ExtendedPermission] ");
            strSql.Append(" where ExtendedPermissionID=@ExtendedPermissionID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExtendedPermissionID", SqlDbType.Int,4)
			};
            parameters[0].Value = ExtendedPermissionID;

            ExtendedPermissionEntity model = new ExtendedPermissionEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ExtendedPermissionEntity DataRowToModel(DataRow row)
        {
            ExtendedPermissionEntity model = new ExtendedPermissionEntity();
            if (row != null)
            {
                if (row["ExtendedPermissionID"] != null && row["ExtendedPermissionID"].ToString() != "")
                {
                    model.ExtendedPermissionID = int.Parse(row["ExtendedPermissionID"].ToString());
                }
                if (row["TypeId"] != null && row["TypeId"].ToString() != "")
                {
                    model.TypeId = int.Parse(row["TypeId"].ToString());
                }
                if (row["Comment"] != null)
                {
                    model.Comment = row["Comment"].ToString();
                }
                if (row["CustomMessage"] != null)
                {
                    model.CustomMessage = row["CustomMessage"].ToString();
                }
            }
            return model;
        }

    }
}
